﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Collections.Generic;
using Microsoft.SharePoint;

namespace TRIG_MOD_STATUSSUMMARY.IR_StatusSummary
{
    public partial class IR_StatusSummaryUserControl : UserControl
    {
        DataForStatusSummary objCls = new DataForStatusSummary();
        SPHelper obj = new SPHelper();

        public IR_StatusSummary ParentWebPart
        {
            get;
            set;
        }
        SPSite oSite;
        SPWeb oWeb;

        List<string> docTypes = new List<string>();
        List<string> discipAbb = new List<string>();

        public List<string> DocTypes
        {
            get { return docTypes; }
            set { docTypes = value; }
        }

        public List<string> Disciplines
        {
            get { return discipAbb; }
            set { discipAbb = value; }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                DocTypes = objCls.GetDocumentType(ddlDocsTypes);
                ddlDocsTypes.Items.Add("Inspection Requests");
                Disciplines = objCls.GetDiscipiline();
            }
            catch (Exception x)
            {
                errorMessage.Text += x.Message;
            }

            DateValidation();
            //Controls.Add(stSumGrid);
            //base.CreateChildControls();
        }

        private void DateValidation()
        {
            stDT.MaxDate = DateTime.Today;
            endDT.MaxDate = DateTime.Today;
            stSumGrid.DataSource = null;
            stSumGrid.Visible = false;
            if (Convert.ToInt32(rdDates.SelectedItem.Value) == 0)
            {
                endDT.Visible = false;
                stDT.IsRequiredField = true;
                stDT.SelectedDate = DateTime.Today;
            }
            else
            {
                endDT.Visible = true;
                stDT.IsRequiredField = true;
                endDT.IsRequiredField = true;
            }
        }

        // Get all Documents and add to generic list and ddlDocsTypes
        // Purpose to get Document Type to anywhere in projects
        public void GetDocumentType()
        {

            using (oSite = new SPSite(SPContext.Current.Site.Url))
            {
                using (oWeb = oSite.OpenWeb())
                {
                    SPList oList = oWeb.Lists.TryGetList("Document Types");
                    SPListItemCollection oLstItemCol = oList.Items;
                    foreach (SPListItem oLstItem in oLstItemCol)
                    {
                        docTypes.Add(Convert.ToString(oLstItem["Title"]));
                        ddlDocsTypes.Items.Add(Convert.ToString(oLstItem["Title"]));
                    }
                }
            }

            //obj.GetListItems("Letters Request Forms", lblErr, "LetterRe", "hr");
        }

        // Bind and upload to grid
        public void GetDocItems(List<string> DocType)
        {
            foreach (string cvlDocs in DocType)
            {
                obj.GetListItems(cvlDocs, errorMessage, oWeb.Title);

            }
        }

        private void NullLabels()
        {
            errorMessage.Text = "";
            stSumGrid.Visible = true;
        }

        private void GetDates(string today)
        {
            string startDt = Convert.ToString(stDT.SelectedDate.ToShortDateString());
            objCls.cpStDT = startDt;
            objCls.cpEndDT = startDt;

        }

        private void GetDates()
        {
            //stDT.IsRequiredField = true;
            //endDT.IsRequiredField = true;
            string startDt = Convert.ToString(stDT.SelectedDate.ToShortDateString());
            string endDt = Convert.ToString(endDT.SelectedDate.ToShortDateString());
            objCls.cpStDT = startDt;
            objCls.cpEndDT = endDt;

        }

        //protected void btnProcess_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        NullLabels();
        //        objCls.rdDates = rdDates;
        //        if (Convert.ToInt32(rdDates.SelectedItem.Value) == 1) // As of Today
        //            GetDates();
        //        else
        //            GetDates("Today");
        //        objCls.grid = stSumGrid;

        //        string Type = "CNT";
        //        if (Type == "CNT")
        //        { objCls.CreateTable("IR_CNT"); objCls.Group = ("CNT"); }
        //        else { objCls.CreateTable("IR_CNS"); objCls.Group = ("CNS"); }

        //        int ck= objCls.GetDocItemsbyDiscipiline_MM(ddlDocsTypes.SelectedItem.Text, Disciplines, errorMessage, "MM");
        //        if (ck > 0)
        //            objCls.BindGrid(stSumGrid, "MM");
        //        else { }
        //    }
        //    catch (Exception x)
        //    {
        //        errorMessage.Text = x.Message ;
        //    }
           

        //}


        protected void SelectedIndexChanged(object sender, EventArgs e)
        {
            stDT.ClearSelection();
            endDT.ClearSelection();
            stSumGrid.DataSource = null;
            stSumGrid.Visible = false;
        }

        protected void btnProcessMM_Click(object sender, EventArgs e)
        {
            try
            {
                NullLabels();
                objCls.rdDates = rdDates;
                if (Convert.ToInt32(rdDates.SelectedItem.Value) == 1) // As of Today
                    GetDates();
                else
                    GetDates("Today");
                objCls.grid = stSumGrid;

                string Type = "CNT";
                if (Type == "CNT")
                { objCls.CreateTable("IR_CNT"); objCls.Group = ("CNT"); }
                else { objCls.CreateTable("IR_CNS"); objCls.Group = ("CNS"); }

                int ck = objCls.GetDocItemsbyDiscipiline_MM(ddlDocsTypes.SelectedItem.Text, Disciplines, errorMessage, "MM");
                if (ck > 0)
                    objCls.BindGrid(stSumGrid, "MM");
                else { }
            }
            catch (Exception x)
            {
                errorMessage.Text = x.Message;
            }
        }

       
    }
}
